package TCP;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

/**
 * Day04_NetWorkInterface 类用于展示如何获取和打印网络接口信息以及其IP地址。
 */
public class Day04_NetWorkInterface {
    public static void main(String[] args) {
        try {
            // 获取系统中所有的网络接口
            Enumeration<NetworkInterface> enumeration = NetworkInterface.getNetworkInterfaces();
            // 遍历每个网络接口，并打印其信息
            while (enumeration.hasMoreElements()) {
                NetworkInterface networkInterface = enumeration.nextElement();

                // 获取并打印每个网络接口的所有子接口
                Enumeration<NetworkInterface> subInterfaces = networkInterface.getSubInterfaces();
                System.out.println(networkInterface);

                // TODO: 可以在此处添加对子接口的处理逻辑
            }
            System.out.println("#########################################################");
            // 展示名称为"loopback_0"的接口的IP地址
            NetworkInterface networkInterface1 = NetworkInterface.getByName("loopback_0");
            // 获取并遍历该接口的所有IP地址
            Enumeration<InetAddress> addressEnumeration = networkInterface1.getInetAddresses();
            while (addressEnumeration.hasMoreElements()) {
                InetAddress inetAddress = (InetAddress) addressEnumeration.nextElement();
                // 打印IP地址
                System.out.println(inetAddress.getHostAddress());
            }

        } catch (SocketException e) {
            // 捕获并打印SocketException异常
            e.printStackTrace();
        }
    }
}
